package jhunter.dao.condition;

import java.util.ArrayList;
import java.util.List;

import jhunter.dao.EntityExt;
import jhunter.dao.type.TypeHandlerFactory;

public class PropertyCondition implements Condition {

	private String operation;

	private Object value;

	private EntityExt entity;

	private String propertyName;

	public PropertyCondition(EntityExt entity, String propertyName, String operation, Object value) {
		this.entity = entity;
		this.propertyName = propertyName;
		this.operation = operation;
		this.value = value;
	}

	@Override
	public String getWhereSql() {
		return entity.getTableAlias() + "." + entity.getColumnName(propertyName) + " " + operation + " ? ";
	}

	@Override
	public List<Object> getParameters() {
		List<Object> list = new ArrayList<Object>(1);
		String type = entity.getPropsMap().get(propertyName).getType();
		list.add(TypeHandlerFactory.getTypeHandler(type).getSqlObject(value));
		return list;
	}

	@Override
	public boolean isEmpty() {
		return false;
	}

	public String getOperation() {
		return operation;
	}

	public void setOperation(String operation) {
		this.operation = operation;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	public EntityExt getEntity() {
		return entity;
	}

	public void setEntity(EntityExt entity) {
		this.entity = entity;
	}

	public String getPropertyName() {
		return propertyName;
	}

	public void setPropertyName(String propertyName) {
		this.propertyName = propertyName;
	}

}
